using System;
using System.Collections.Generic;
using iPhOwa.Exchange.ContentClasses;
using iPhOwa.Exchange.ContentClasses.Fields;
using iPhOwa.Exchange.WebDav.Methods;

namespace iPhOwa.Exchange.Managers
{
	/// <summary>
	/// Encapsulates working with folders.
	/// </summary>
	public static class FolderManager
	{
		/// <summary>
		/// Get a single folder.
		/// </summary>
		public static Folder Get(Uri path, OwaSession session)
		{
			var properties = new[]
			                 	{
			                 			ItemFields.Href
			                 			, ItemFields.ParentName
			                 			, ItemFields.DisplayName
			                 			, FolderFields.UnreadCount
			                 	};

			var currentFolderPropFind = new PropFind(path, session, properties);

			return currentFolderPropFind.Run() as Folder;
		}

		/// <summary>
		/// Get a list of folders.
		/// </summary>
		/// <returns></returns>
		public static ICollection<Folder> GetList(Uri path, OwaSession session)
		{
			var properties = new[]
			                 	{
			                 			ItemFields.Read
			                 			, ItemFields.Href
			                 			, ItemFields.ParentName
			                 			, ItemFields.DisplayName
			                 			, FolderFields.UnreadCount
			                 			, ItemFields.HasSubs
			                 	};

			var search = new Search(path, session, properties) {IsFolder = true};

			var items = new List<Item>(search.Run()).ConvertAll(i => i as Folder);

			items.Sort(( x, y ) => x.DisplayName.CompareTo(y.DisplayName));

			return items;
		}
	}
}